import numpy as np
import pandas as pd
from pandas import Series, DataFrame

# 测试Series的排序
s1 = Series( np.random.randn( 5 ) )

s2 = s1.sort_values()
print( s2 )
'''
4   -1.545745
1   -1.027576
2   -0.378346
3   -0.173559
0   -0.034169
'''
s2 = s2.sort_index() # 如果按照降序，将参数 ascending = False 即可
print( s2 )
'''
0   -0.034169
1   -1.027576
2   -0.378346
3   -0.173559
4   -1.545745
'''

# 测试 DataFrame 的排序
df = DataFrame( np.random.randn( 25 ).reshape( 5, 5 ), columns = [ "A", "B", "C", "D", "E" ] )
print( df )
'''
          A         B         C         D         E
0  0.008747  0.222510  0.073873 -0.262537  1.046503
1  1.705621 -1.189569 -0.738786  1.070742  0.079151
2  0.198840  0.554127  0.226496  0.440976 -0.486588
3 -0.034353  1.492573 -0.117536  1.343469  0.001695
4  0.471091  0.197481 -1.700222 -0.467749 -1.205235
'''
# 按照A列进行排序
df2 = df.sort_values( "A" )
print( df2 )
'''
          A         B         C         D         E
1 -0.666340  0.785221 -0.554947  0.321611 -1.310909
2 -0.243319  0.138248  2.280287 -0.026741 -0.096120
0  0.555123 -0.098540 -0.411645  0.136957  0.277404
3  1.526962  1.645865  1.169431  2.070103  1.536584
4  2.524886  0.512343  0.967945  1.090171  0.593123
'''
df3 = df2.sort_index()
print( df3 )
'''
          A         B         C         D         E
0 -1.327867 -1.025205 -1.108822  0.709976  1.548124
1 -0.563930  0.442501  0.647772 -0.008351 -0.598328
2 -0.693583  0.313214  0.476824 -0.230287  0.827252
3  0.598216  0.874092 -0.051866 -0.449454 -0.692436
4  0.785472 -0.819589 -0.579758  0.816789 -0.684915
'''